import 'package:flutter/material.dart';
import 'package:flutter_rider/module/mine/api/truck_api.dart';
import 'package:flutter_rider/module/mine/models/truck_model.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_rider/generated/r.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/state/state_layout.dart';
import 'package:flutter_rider/module/mine/provider/outside_car_provider.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/utils/screen_fit.dart';

class OutsideStationsWidget extends ConsumerStatefulWidget {
  // StopStationProvider provider;
  OutsideStationsWidget({
    Key? key,
    // required this.provider,
  }) : super(key: key);
  @override
  ConsumerState createState() => _OutsideStationsWidgetState();
}

class _OutsideStationsWidgetState extends ConsumerState<OutsideStationsWidget> {
  final List<TruckParkModel> _result = [];
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // context.read<StopStationProvider>();
    return Container(
      height: 440.rpx,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(8.rpx)),
      ),
      child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
        8.hspace,
        Container(
          width: 36.rpx,
          height: 5.rpx,
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.26),
            borderRadius: BorderRadius.circular(2.rpx),
          ),
        ),
        8.hspace,
        Container(
          alignment: Alignment.center,
          child: Row(children: [
            16.wspace,
            InkWell(
                child: SvgPicture.asset(R.image_left,
                    width: 24.rpx, height: 24.rpx),
                onTap: () {
                  NavigatorUtils.goBack(context);
                }),
            // ),
            4.wspace,
            Expanded(
                child: Container(
              alignment: Alignment.center,
              child:
                  UText.get("停靠点选择", Colors.black, 20.rpx, fw: FontWeight.bold),
            )),
            4.wspace,
            24.wspace,
            16.wspace,
          ]),
        ),
        18.hspace,
        Expanded(
          child: StateLayout(
            dataCallback: _onDataCallback,
            url: TruckApi.truckPark,
            childBuilder: _renderContent,
          ),
        ),
      ]),
    );
  }

  _onDataCallback(dynamic data) {
    List list = data;
    _result.addAll(list.map((e) => TruckParkModel.fromMap(e)).toList());
  }

  Widget _renderContent(dynamic data) {
    if (_result.isEmpty) {
      return Center(
        child: UText.getSubText("暂无数据内容"),
      );
    }
    return ListView.separated(
      itemBuilder: _renderItem,
      itemCount: _result.length,
      separatorBuilder: _renderSeparator,
    );
  }

  Widget _renderSeparator(BuildContext context, int index) {
    return 0.5.hLine16;
  }

  Widget _renderItem(BuildContext context, int index) {
    var item = _result[index];
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Container(
          padding: EdgeInsets.all(16.rpx),
          child:
              UText.get(item.name, Colors.black, 16.rpx, fw: FontWeight.w600)),
      onTap: () {
        ref.read(stopStationProvider.state).state = item;
        NavigatorUtils.goBack(context);
      },
    );
  }
}
